package orderProcessingProcess.server;

import orderProcessingProcess.model.Customer;
import orderProcessingProcess.dataStorage.DataBackend;
import orderProcessingProcess.util.AIC_log;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;


/** Customer Management Service (RESTfull Web Service) 
 * 
 * @author tom
 *
 */
public class CustomerManagementServiceImpl implements CustomerManagementService {
	DataBackend dataBackend;
	@Context HttpHeaders requestHeaders;
	static final String SERVICENAME = "CustomerManagementService";
	
	public CustomerManagementServiceImpl() {
		dataBackend = DataBackend.getInstance();
	};
	
	/** get customer data
	 * @param id the customer id
	 * @return the customer object if found, Response.Status.NOT_FOUND otherwise
	 */
	public Customer getCustomer(String id) {
		Customer customer = dataBackend.getCustomerById(id);

		// try to retrieve customer information
		if (customer != null) {
		    AIC_log.print( SERVICENAME, "Retrieving customer data for id " + id + ", format: " + requestHeaders.getAcceptableMediaTypes()); 
			return customer;
		}
		else {
			AIC_log.printerr( SERVICENAME, "Customer data for id " + id + " - not found" );
			throw new WebApplicationException(Response.Status.NOT_FOUND);			
		}
	}
	
	/** create a new customer
	 * @param customer
	 * @return Response.Status.OK if the customer was added, Response.Status.NOT_MODIFIED otherwise
	 */
	public Response addCustomer(Customer customer) {		
		String id = customer.getId();

		// check whether the customer exists
		if (id != null && dataBackend.getCustomerById(id) != null) {
			AIC_log.printerr ( SERVICENAME, "Failed to add customer with id " + id + " - customer exists" );
			return Response.status(Response.Status.NOT_MODIFIED).build();
		}

		// add the customer
		if (dataBackend.addCustomer(customer)) {
			AIC_log.print( SERVICENAME, "Added customer with id " + id + ", format: " + requestHeaders.getMediaType() );
			return Response.ok().build();
		} else {
			AIC_log.printerr ( SERVICENAME, "Failed to add customer - storage error" );
			return Response.status(Response.Status.NOT_MODIFIED).build();
		}
	}
		
	/** update customer data
	 * @param id id of the customer to be updated, must be equal to customer.getId()
	 * @param customer new customer data
	 * @return Response.Status.OK if the customer data was updated, Response.Status.NOT_FOUND if there is no customer to update, Response.Status.NOT_MODIFIED if the update failed
	 */
	public Response updateCustomer(String id, Customer customer) {
		// check whether the customer id of the object matches the request id
		if (! id.equals(customer.getId())) {
			AIC_log.printerr( SERVICENAME,  "Failed to add customer with id " + id + " - id conflict" );
			return Response.status(Response.Status.NOT_MODIFIED).build();
		}

		// check whether the customer exists
		if (dataBackend.getCustomerById(id) == null) {
			AIC_log.printerr( SERVICENAME,  "Failed to update customer with id " + id + " - not found" );
			return Response.status(Response.Status.NOT_FOUND).build();
		}

		// update customer data
		if (dataBackend.addCustomer(customer)) {
			AIC_log.print( SERVICENAME,  "Updated customer " + id + ", format: " + requestHeaders.getMediaType());
			return Response.ok().build();
		} else {
			AIC_log.printerr( SERVICENAME, "Failed to update customer with id " + id + " - storage error" );
			return Response.status(Response.Status.NOT_MODIFIED).build();
		}
	}
	
	/** delete the customer from the database
	 * @param id id of the customer to be deleted
	 * @return Response.Status.OK if the customer was deleted, Response.Status.NOT_FOUND otherwise
	 */
	public Response deleteCustomer(String id) {

		// try to delete customer
		if (dataBackend.deleteCustomerById(id)) {
			AIC_log.print( SERVICENAME,  "Deleted customer " + id);
			return Response.ok().build();
		} else {
			AIC_log.printerr( SERVICENAME,  "Failed to delete customer with id " + id + " - not found");
			return Response.status(Response.Status.NOT_FOUND).build();
		}
	}
	

}
